﻿using System.Collections.Generic;

namespace Knapsack
{
    public class Knapsack
    {
        private Dictionary<EProp, List<Prop>> map = new();
        private bool this[EProp type] => map.ContainsKey(type);

        public bool Add(EProp type,int id,int count)
        {
            if (this[type] == false)
                return false;

            var prop = FindPropById(map[type],id);
            if (prop == null)
            {
                prop = Create(type,id);
                map[type].Add(prop);
            }

            prop.count += count;
            return true;
        }

        public bool Use(EProp type,int id,int count)
        {
            if (this[type] == false)
                return false;
            var prop = FindPropById(map[type],id);
            if (prop == null)
                return false;
            if (prop.count < count)
                return false;
            prop.count -= count;
            if(prop.count == 0)
                map[type].Remove(prop);
            return true;
        }

        private Prop Create(EProp type,int id)
        {
            var prop = new Prop();
            prop.type = type;
            prop.id = id;
            prop.count = 0;
            prop.name = "";
            prop.des = "";
            return prop;
        }
        private Prop FindPropById(List<Prop> props,int id)
        {
            return props.Find(p => p.id == id);
        }
    }
}